#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "system_types.h"
#include "user_management.h"
#include "package_management.h"
#include "inventory_management.h"
#include "noticement_system.h"
#include "notification_system_UI.h"
#include "user_interface_UI.h"

// 主函数
int main() {
    // 初始化系统
    SystemData* data = initSystem();
    if (data == NULL) {
        printf("系统初始化失败!\n");
        return -1;
    }

    // 加载系统数据
    if (loadSystemData(data) != OK) {
        printf("加载系统数据失败!\n");
        cleanupSystem(data);
        return -1;
    }

    // 简单登录界面
    User* currentUser = NULL;
    char username[MAX_USERNAME_LEN];
    char password[MAX_PASSWORD_LEN];
    char phone[MAX_PHONE_LEN];
    char address[MAX_ADDRESS_LEN];

    while (1) {
        system("cls");
        printf("\n校园智能快递集散系统\n");
        printf("1. 登录\n");
        printf("2. 注册\n");
        printf("3. 关于本系统\n");
        printf("0. 退出\n");
        printf("\n请选择: ");

        int choice;
        scanf("%d", &choice);
        getchar();  // 吸收换行符

        if (choice == 0) {
            break;
        }
        else if (choice == 1) {
            currentUser = handleUserLogin(data);
            if (currentUser != NULL) {
                printf("登录成功!\n");
            }
        }
        else if (choice == 2) {
            ErrorCode result = handleUserRegistration(data);
            if (result == OK) {
                printf("注册成功，请登录!\n");
            }
            else {
                printf("注册失败，错误码: %d\n", result);
            }
        }
        else if (choice == 3) {
            system("cls");
            printf("\n==========================================================================\n");
            printf("关于校园智能快递集散系统\n");
            printf("开发者名单:\n-陈辰(组长): 负责系统结构设计和消息模块\n-张永康: 负责用户管理模块\n-刘根森: 负责包裹和库存管理模块\n");
            printf("版本: 1.0.0\n");
            printf("开发时间: 2025年3月\n");
            printf("功能简介:\n");
            printf("- 用户管理: 支持普通用户、VIP用户、管理员、快递员和驿站工作人员\n");
            printf("- 包裹管理: 入库、出库、查询、计费等功能\n");
            printf("- 通知系统: 自动推送包裹状态和系统通知\n");
            printf("==========================================================================\n");
            printf("\n按任意键返回主菜单...");
            getchar();
        }


        // 主循环
        if (currentUser != NULL) {
            int running = 1;

            while (running) {
                system("cls");
                int choice;
                UserType userType = currentUser->type;

                // 根据用户类型显示不同菜单
                switch (userType) {
                case USER_ADMIN:
                    showAdminUserMenu(currentUser);
                    break;

                case USER_STUDENT:
                case USER_VIP:
                    showRegularUserMenu(currentUser);
                    break;

                case USER_COURIER:
                    showCourierUserMenu(currentUser);
                    break;

                case USER_STAFF:
                    showStaffUserMenu(currentUser);
                    break;

                default:
                    printf("未知用户类型！\n");
                    running = 0;
                    continue;
                }

                scanf("%d", &choice);
                getchar();  // 吸收换行符

                // 处理普通/VIP用户的菜单选择
                if (userType == USER_STUDENT || userType == USER_VIP) {
                    switch (choice) {
                    case 1: // 查看我的包裹信息
                        userPackageMenu(data, currentUser);
                        break;
                    case 2: // 查看我的通知
                        showNotificationConsole(data, currentUser);
                        break;
                    case 3: // 查看个人用户信息
                        handleViewUserInfo(data, currentUser);
                        printf("\n按回车键继续...");
                        getchar();
                        break;
                    case 4: // 修改个人信息
                        handleUpdateUserInfo(data, currentUser);
                        break;
                    case 5: // 注销账户
                        if (handleDeleteUser(data, currentUser)) {
                            printf("账户已成功注销。\n");
                            running = 0;
                            currentUser = NULL;
                        }
                        break;
                    case 6: //开通VIP
                        if (currentUser->type = USER_VIP) {
                            printf("您已经是VIP用户！不可重复开通！");
                            getchar();
                            break;
                        }
                        printf("99元终身会员，VIP用户可以享受包裹，寄件优惠！确定要开通吗？\n");
                        printf("确认开通请输入 1, 不开通输入 0: \n");
                        int vip;
                        scanf("%d", &vip);
                        if (vip) {
                            currentUser->type = USER_VIP;
                            currentUser->fee -= 99;
                            printf("开通会员成功！请在系统内缴费！\n");
                        }
                        else {
                            printf("已退出开通VIP！\n");
                        }
                        printf("按回车键继续");
                        getchar();
                        break;
                        //开通VIP功能
                    case 0: // 返回主菜单
                        running = 0;
                        currentUser = NULL;
                        break;
                    default:
                        printf("无效选择，请重试!\n");
                        printf("按回车键继续...");
                        getchar();
                    }
                }
                // 处理管理员的菜单选择
                else if (userType == USER_ADMIN) {
                    if (choice >= 1 && choice <= 9) {
                        if (!handleAdminUserManagement(data, currentUser, choice)) {
                            running = 0;
                            currentUser = NULL;
                        }
                    }
                    else if (choice == 0) {
                        running = 0;
                        currentUser = NULL;
                    }
                    else {
                        printf("无效选择，请重试!\n");
                        printf("\n按回车键继续...");
                        getchar();
                    }
                }
                // 处理快递员的菜单选择
                else if (userType == USER_COURIER) {
                    switch (choice) {
                    case 1: // 包裹入库
                        courierPackageMenu(data, currentUser);
                        break;
                    case 2: // 查看我的派送记录
                    {
                        int packageCount = 0;
                        Package** packages = findPackagesByCourier(data, currentUser->phone, &packageCount);

                        if (packages != NULL && packageCount > 0) {
                            printf("\n===== 我的派送记录 =====\n");
                            printf("共找到 %d 个包裹\n\n", packageCount);

                            for (int i = 0; i < packageCount; i++) {
                                char* packageInfo = getPackageInfoString(packages[i]);
                                printf("%s\n", packageInfo);
                                free(packageInfo);
                            }

                            free(packages);
                        }
                        else {
                            printf("没有找到派送记录。\n");
                        }

                        printf("\n按回车键继续...");
                        getchar();
                    }
                    break;
                    case 3: // 查看个人用户信息
                        handleViewUserInfo(data, currentUser);
                        printf("\n按回车键继续...");
                        getchar();
                        break;
                    case 4: // 修改个人信息
                        handleUpdateUserInfo(data, currentUser);
                        break;
                    case 5: // 注销账户
                        if (handleDeleteUser(data, currentUser)) {
                            printf("账户已成功注销。\n");
                            running = 0;
                            currentUser = NULL;
                        }
                        break;
                    case 0: // 返回主菜单
                        running = 0;
                        currentUser = NULL;
                        break;
                    default:
                        printf("无效选择，请重试!\n");
                        printf("\n按回车键继续...");
                        getchar();
                    }
                }
                // 处理工作人员的菜单选择
                else if (userType == USER_STAFF) {
                    switch (choice) {
                    case 1: // 包裹管理
                        packageManagementMenu(data, currentUser);
                        break;
                    case 2: // 货架管理
                        // 货架管理功能调用
                        printf("货架管理功能暂未实现\n");
                        printf("\n按回车键继续...");
                        getchar();
                        break;
                    case 3: // 用户查询
                    {
                        char queryPhone[MAX_PHONE_LEN];
                        printf("请输入要查询的用户手机号: ");
                        scanf("%s", queryPhone);
                        getchar(); // 吸收换行符

                        User* user = findUserByPhone(data, queryPhone);
                        if (user != NULL) {
                            printf("\n用户信息:\n");
                            printf("用户名: %s\n", user->username);
                            printf("手机号: %s\n", user->phone);
                            printf("地址: %s\n", user->address);
                            printf("用户类型: %d\n", user->type);
                        }
                        else {
                            printf("未找到该用户\n");
                        }

                        printf("\n按回车键继续...");
                        getchar();
                    }
                    break;
                    case 4: // 查看个人用户信息
                        handleViewUserInfo(data, currentUser);
                        printf("\n按回车键继续...");
                        getchar();
                        break;
                    case 5: // 修改个人信息
                        handleUpdateUserInfo(data, currentUser);
                        break;
                    case 6: // 注销账户
                        if (handleDeleteUser(data, currentUser)) {
                            printf("账户已成功注销。\n");
                            running = 0;
                            currentUser = NULL;
                        }
                        break;
                    case 0: // 返回主菜单
                        running = 0;
                        currentUser = NULL;
                        break;
                    default:
                        printf("无效选择，请重试!\n");
                        printf("\n按回车键继续...");
                        getchar();
                    }
                }
            }
        }
    }

    // 保存系统数据
    saveSystemData(data);

    // 清理系统
    cleanupSystem(data);

    return 0;
}